/**
 *
 */
var path = '../';
var pageListUrl = path + 'storeHandling/list';
var pageUrl = path + 'storeHandling/';

$(function () {
    $(document).keyup(function (e) {
        if (e.which == 13) {
            $('#selectBtnEnter').click();
        }
    })
});
//添加弹窗
var addPanel = null;
// ---------------交互代码begin-----------------
//本页面业务逻辑
var dictType = {
    checkLogic: function (dataObj) {
        var result = "";
        var isSubmit = true;
        console.log(dataObj);
        var equipmentCode = dataObj.equipmentCode;
        //	var reg = /^[0-9]{1,25}$/;
        var reg = /^\s*$/g;
        if (reg.test(equipmentCode)) {
            dataObj.equipmentCodeErr = "设备编号不能为空";
            isSubmit = false;
        }
        var postionCode = dataObj.postionCode;
        //var reg = /^([0-9A-Fa-f]{2}:){5}(([0-9A-Fa-f]{2}))$/;
        var reg = /^\s*$/g;
        if (postionCode.length == 0 && reg.test(postionCode)) {
            dataObj.postionCodeErr = "位置编号不能为空";
            isSubmit = false;
        }

        $.ajax({
            url: pageUrl + "check",
            type: "POST",
            dataType: "json",
            data: {postionCode: dataObj.postionCode, equipmentCode: dataObj.equipmentCode},
            cache: false,
            async: false
        }).done(function (res) {
            if (res.result == 'postionCode') {
                dataObj.postionCodeErr = '位置编号不存在';
                isSubmit = false;
            } else if (res.result == 'equipmentCode') {
                dataObj.equipmentCodeErr = "设备编号已存在";
                isSubmit = false;
            }
        });

        result = customUtils.checkLen(dataObj.status, 1, 1, '设备状态');
        if (result.length != 0) {
            dataObj.statusErr = result;
            isSubmit = false;
        }
        result = customUtils.checkLen(dataObj.installBy, 1, 30, '安装人');
        if (dataObj.installBy && dataObj.installBy.length > 0 && result.length != 0) {
            dataObj.installByErr = "不能超过30位";
            isSubmit = false;
        }
        return isSubmit;
    },
    checkFileType: function () {
        var fileName = $("#file")[0].files[0].name;
        var fileType = fileName.substr(fileName.lastIndexOf(".") + 1, fileName.length);
        if (fileType.toUpperCase() != 'XLSX' && fileType.toUpperCase() != 'XLS') {
            importVue.fileErr = "请选择*.xlsx或者*.xls格式的Excel文件";
            return false;
        }
        return true;
    },
    checkFiled: function () {
        if ($("#filed")[0].files.length <= 0) {
            importVue1.fileErr = "请选择导入的设备文件";
            result = false;
        }
        var fileName = $("#filed")[0].files[0].name;
        var fileType = fileName.substr(fileName.lastIndexOf(".") + 1, fileName.length);
        if (fileType.toUpperCase() != 'XLSX' && fileType.toUpperCase() != 'XLS') {
            importVue1.fileErr = "请选择*.xlsx或者*.xls格式的Excel文件";
            return false;
        }
        return true;
    },
    checkFiled1: function () {
        if ($("#filed1")[0].files.length <= 0) {
            importVue2.fileErr = "请选择导入的设备文件";
            result = false;
        }
        var fileName = $("#filed1")[0].files[0].name;
        var fileType = fileName.substr(fileName.lastIndexOf(".") + 1, fileName.length);
        if (fileType.toUpperCase() != 'XLSX' && fileType.toUpperCase() != 'XLS') {
            importVue2.fileErr = "请选择*.xlsx或者*.xls格式的Excel文件";
            return false;
        }
        var val = importVue2.isUploadFace;
        if (importVue2.isUploadFace.length > 0 || importVue2.isUploadScreenshots.length > 0 || importVue2.isUploadEnvironment.length > 0) {
            return true;
        } else {
            importVue2.statusErr = "请至少选择一个图片上传修改控制";
            return false;
        }
    }
};
//绑定列表List---begin----所有的操作都围绕列表展开，新增、更新其他比较独立
var dataList = new Vue({
        el: "#storeExceptionHandlingList",
        data: {
            ids: [],
            pageInfo: {},
            fileErr: "",
            //自定义条件查询---begin--------
            status: "-1",
            key: "",
            id: "",
            all: false,
            currentDate: "",
            equipmentCode: "",
            shopName: "",
            errorPhenomenon: "",
            phoneCommunicationRecord: "",
            instructorCommunicationRecord: "",
            visitRecord: "",
            errorReason: "",
            errorPhenomenon: "",
            processingMethod: "",
            num: "",
            total: "",
            processingResult: "",
            msg: {
                sysErrorMsg: "",
                checkErrMsg: "",
                fieldErrMsg: "",
                success: ""
            },
            provinces: [],
            citys: [],
            areas: [],
            //自定义条件查询---end--------
            selectIds: [],//多选
            selectedIdx: -1,//保存操作行的索引
            eventSrc: "",//init:页面初始化；search:点击查询；paging:点击翻页；changePageSize:改变页面显示条数
            selectedPageSize: customUtils.pageSize//修改页面数据显示条数
        }, mounted: function () {
            //初始化页面数据
            this.getList('init');
        },
        // 在 `methods` 对象中定义方法,如果操作中函数的逻辑比较复杂，必须封装到dictType对象
        methods: {
            selectChkBtn: function (e) {//用于多选事件
                this.selectIds.push($(e.target).val());
            },
            changeTime: function (elem) {
                this.$data[elem.split("-")[0]] = $("#" + elem).val();
            },
            //搜索、分页列表
            getList: function (eventSrc) {
                //保存事件源
                this.eventSrc = eventSrc;
                //改变页面显示条数
                if (eventSrc == "changePageSize") {
                    if (this.pageInfo.pageSize != this.selectedPageSize) {
                        this.pageInfo.pageSize = this.selectedPageSize;
                        $('#pagination').jqPaginator('destroy');
                        eventSrc = 'init';//重新绘制控件
                    } else {
                        return;
                    }
                }
                var param = {"page": 1, "rows": customUtils.pageSize};
                //初始化时无当前页数，后台有默认第1页，每页10条
                if (this.pageInfo != undefined && this.pageInfo.pageNum != undefined) {
                    param["page"] = this.pageInfo.pageNum;
                    param["rows"] = this.pageInfo.pageSize;
                }
                this.$data.ids = [];
                //TODO 搜索条件
                param["shopName"] = this.$data.shopName;
                param["equipmentCode"] = this.$data.equipmentCode;
                console.log(param);
                this.$http.post(pageListUrl, param).then(function (response) {
                    dataList.pageInfo = response.body.pagination;
                    dataList.total = response.body.total;
                    customUtils.noData('无数据', dataList.pageInfo.pages);
                    //页面初始化、搜索时，才需要执行分页控件
                    if (eventSrc == 'init' || eventSrc == 'search') {
                        customUtils.paging('#pagination', dataList.pageInfo.pages, customUtils.showPage, function (num) {
                            //当前页码pageNum与将要翻页的页码num不相等才执行，否则为同一页
                            if (dataList.pageInfo.pageNum != num) {
                                dataList.pageInfo.pageNum = num;
                                dataList.getList('paging');
                            }
                        });
                    }
                }, function (response) {
                    console.log(response.body);
                });
            },
            //全选--取消全选方法
            checkAll: function () {
                var fg = this.$data.all;
                if (fg) {
                    this.$data.all = false;
                    $("tbody").find("input[type='checkbox'][name='id']").each(function (i, val) {
                        dataList.$data.ids = [];
                    });
                } else {
                    this.$data.all = true;
                    $("tbody").find("input[type='checkbox'][name='id']").each(function (i, val) {
                        dataList.$data.ids[i] = val.value;
                    });
                }
                console.log(this.$data.ids)
            },
            //批量删除
            deleteAllSE: function () {
                console.log(this.$data.ids);
                var size = dataList.ids.length;
                if(size <= 0){
                    this.$data.msg.checkErrMsg="请勾选记录";
                    customUtils.showTips(this.$data.msg,null,3000);
                    return;
                }
                var msg = "您确定要将" + size + " 条门店异常处理记录删除吗";
                var self = this;
                layer.confirm(msg, function (index) {
                    var param = {};
                    param["items"] = JSON.stringify(dataList.ids);
                    customUtils.post(self, pageUrl + "deleteAllSE", param, function (response, isSuccess) {
                        customUtils.showTips(response.body,null,3000);
                        dataList.getList('no');
                    }, function (response) {
                        customUtils.showRequestTips(response, 3000)
                    });
                    layer.close(index);
                    return true;
                });
                return false;
            },
            openImportPanel: function () {
                var obj3 = document.getElementById('file');
                obj3.outerHTML = obj3.outerHTML;
                importVue.fileErr = "";
                importPanel = layer.open({
                    type: 1,
                    title: "导入门店异常信息",
                    area: ['600px', '70%'],
                    shadeClose: false, // 点击遮罩关闭
                    content: $('#importPanel')
                });
            }
        }
    }
    );


//绑定列表List---end----

//新增或更新---------end-------

var importVue = new Vue({
    el: "#importPanel",
    data: {
        fileErr: ""
    },
    methods: {
        importEquipment: function () {
            var self = this;
            if (!dictType.checkFileType()) {
                return;
            }
            var formDate = new FormData($("#importPanel")[0]);
            //没选择文件时
            if ($("#importPanel").find("#file")[0].files.length <= 0) {
                $(".errMsg").html("请选择要上传的数据文件");
                return;
            }
            customUtils.post(self, pageUrl + "upload", formDate, function (response) {
                console.log(response);
                var html = "";

                if (response.body.errList != 'undefined' && response.body.errList != '' && response.body.errList != null) {
                    response.body.errList.forEach(function (value, index) {
                        html = html + value + "<br>";
                    });
                    $(".errMsg").html(html);
                } else {
                    //
                    if (importPanel) layer.close(importPanel);
                    dataList.getList('init');
                }
                //非字段错误提示  
                if (response.body.successMsg) {
                    customUtils.tips(response.body.successMsg, 1, 3000);
                } else {
                    customUtils.showTips(response.body, null, 2000);
                }

            }, function (response) {
                customUtils.showRequestTips(response, 3000)
            })
            console.log(formDate);
        }
    }
});
